#include <opencv2/opencv.hpp>
 
int main() {
    // 加载左图和右图
    cv::Mat left = cv::imread("/home/chyang/Documents/program/slam/chapter_03/src/left.png", cv::IMREAD_GRAYSCALE);
    cv::Mat right = cv::imread("/home/chyang/Documents/program/slam/chapter_03/src/right.png", cv::IMREAD_GRAYSCALE);
    if (left.empty() || right.empty()) {
        std::cerr << "Error: 图像加载失败." << std::endl;
        return -1;
    }
 
    // 创建视差图
    cv::Mat disparity;
 
    // 使用StereoBM算法
    // cv::Ptr<cv::StereoBM> stereoBM = cv::StereoBM::create(16,9);  // 创建StereoBM对象，调整参数以提高精确度
    // stereoBM->compute(left, right, disparity);  // 计算视差图
    cv::Ptr<cv::StereoSGBM> stereoSGBM = cv::StereoSGBM::create(0, 16, 5);  // 调整参数以提高精确度
    stereoSGBM->compute(left, right, disparity);  
 
    // 显示视差图
    cv::imwrite("opencv_demo.bmp", disparity); 
    return 0;
}